/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import org.slideme.android.manager.discovery.DiscoveryContext;
import org.slideme.android.manager.download.DownloadContext;
import org.slideme.android.manager.respoitory.*;

import java.io.IOException;


/**
 * Provides methods for obtaining varous SAM Contexts.
 */
public final class ApplicationManagerContext {

    private Context context;

    private DiscoveryContext discoveryContext;

    private DownloadContext downloadContext;

    private ContentBundleRepository contentBundleRepository;

    private UserInfoRepository userInfoRepository;

    private String catalogServerURI;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public String getCatalogServerURI() {
        return catalogServerURI;
    }

    public void setCatalogServerURI(String catalogServerURI) {
        this.catalogServerURI = catalogServerURI;
    }

    public ContentBundleRepository getContentBundleRepository() {
        return contentBundleRepository;
    }

    public UserInfoRepository getUserInfoRepository() {
        return userInfoRepository;
    }

    public DownloadContext getDownloadContext() {
        return downloadContext;
    }

    public DiscoveryContext getDiscoveryContext() {
        return discoveryContext;
    }

    public Context getAndroidContentContext() {
        return context;
    }

    public void init(Context context, DownloadContext downloadContext, DiscoveryContext discoveryContext,
                     String catalogServerURI)
            throws IOException {
        if (context == null) {
            throw new IllegalArgumentException("context");
        }
        this.context = context;

        if (downloadContext == null) {
            this.downloadContext = DownloadContext.Factory.createAndroidDownloadContext("application/x-android-extern");
            this.downloadContext.init(context);
        }

        if (discoveryContext == null) {
            this.discoveryContext = DiscoveryContext.Factory.createDefaultDiscoveryContext();
            this.discoveryContext.init(this);
        }
        this.catalogServerURI = catalogServerURI;

        DatabaseConfig databaseConfig = new DatabaseConfig();
        databaseConfig.setDatabaseName("slideme.db");
        databaseConfig.setVersion(10);

        DatabaseOpenHelper databaseHelper = new DatabaseOpenHelper();
        SQLiteDatabase database = databaseHelper.openDatabase(context, databaseConfig.getDatabaseName(), null,
                databaseConfig.getVersion());

        ContentBundleContentProvider contentBundleContentProvider = new ContentBundleContentProvider();
        contentBundleContentProvider.init(context, database);
        contentBundleRepository = new ContentBundleRepository();
        contentBundleRepository.init(contentBundleContentProvider);

        /**
        SettingsInfoContentProvider settingsInfoContentProvider = new SettingsInfoContentProvider();
        settingsInfoContentProvider.init(context, database);
        settingsInfoRepository = new SettingsInfoRepository();
        settingsInfoRepository.init(settingsInfoContentProvider);
        */
        InstalledBundleContentProvider installedBundleContentProvider = new InstalledBundleContentProvider();
        installedBundleContentProvider.init(context, database);
        userInfoRepository = new UserInfoRepository();
        userInfoRepository.init(installedBundleContentProvider);
    }

    public void init(Context context, String catalogServerURI) throws IOException {
        init(context, null, null, catalogServerURI);
    }
}
